﻿using System.IO;
using NUnit.Framework;
using QFXProxy.Model;

namespace QFXProxy.Tests
{
    [TestFixture]
    public class FileConverterTest
    {
        #region Public Tests
        [Test]
        public void TestOfx1Format()
        {
            var testFile = @"TestData\Ofx1Format.ofx";
            var expectedFile = @"TestData\Ofx1Format.ofx.expected.qfx";

            ConversionTest(testFile, expectedFile);
        }

        [Test]
        public void TestOfx2Format()
        {
            var testFile = @"TestData\Ofx2Format.ofx";
            var expectedFile = @"TestData\Ofx2Format.ofx.expected.qfx";

            ConversionTest(testFile, expectedFile);
        }
        #endregion

        #region Private Methods
        private static void ConversionTest(string testFile, string expectedFile)
        {
            var testFileInfo = new FileInfo(testFile);
            Assert.That(File.Exists(testFileInfo.FullName));

            var expectedFileInfo = new FileInfo(expectedFile);
            Assert.That(File.Exists(expectedFileInfo.FullName));

            // Converts to QFX
            var converter = new FileConverter { SourceFile = testFileInfo.FullName };
            converter.Convert();

            using (TextReader resultsReader = new StreamReader(converter.DestinationFile))
            using (TextReader expectedReader = new StreamReader(expectedFileInfo.FullName))
            {
                string result;
                string expected;
                do
                {
                    result = resultsReader.ReadLine();
                    expected = expectedReader.ReadLine();

                    // Assert the converted file is as expected.
                    Assert.AreEqual(expected, result);
                } while (result != null && expected != null); 
            }
        }
        #endregion
    }
}
